import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2024-08-05
 * Time: 12:17
 */


public class MyArray implements IList{
    private int usedSize = 10;
    private int[] array = new int[]{1,2,3,4,5,6,7,8,9,0};
    private void checkPos(int pos) throws PosException{
        if(pos > usedSize || pos < 0){
            throw new PosException(" Pos插入位置错误");
        }
    }
    private boolean isFull(){
        return usedSize == array.length;
    }
    private void grow(){
        this.array = Arrays.copyOf(this.array,2 * this.array.length);
    }

    @Override
    public void show() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
    }

    @Override
    public void addAtPos(int pos, int value) {
        try{
            checkPos(pos);

            if(isFull()){
                grow();
            }

            for (int i = usedSize; i > pos; i--) {
                this.array[i] = this.array[i - 1];
            }
            this.array[pos] = value;
            usedSize++;

        }catch (PosException e){
            e.printStackTrace();
            System.out.println("Pos未位置错误");
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.array[i] == toFind){
                return  i;
            }
        }
        return -1;
    }
}
